﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AwesomeEnginePC.Inputs
{
    public class Xbox360Inputs : BaseInput
    {
        private int _gamePadIndex;
        private GamePadState _prevState;

        public Xbox360Inputs(int index)
        {
            _gamePadIndex = index;
        }

        public override Dictionary<object, ButtonState> GetStates()
        {
            Dictionary<object, ButtonState> returnStates = new Dictionary<object, ButtonState>();
            GamePadState currentState = GamePad.GetState((PlayerIndex)_gamePadIndex);

            if (!currentState.IsConnected) return null;

            foreach (Buttons key in Enum.GetValues(typeof(Buttons)))
            {
                returnStates[key] = currentState.IsButtonDown(key) ? ButtonState.Pressed : ButtonState.Released;
            }

            return returnStates;
        }

        public override List<object> JustPressedButtons()
        {
            List<object> returnButtons = new List<object>();
            GamePadState currentState = GamePad.GetState((PlayerIndex)_gamePadIndex);

            if (!currentState.IsConnected) return null;

            foreach (Buttons key in Enum.GetValues(typeof(Buttons)))
            {
                if (currentState.IsButtonDown(key) && _prevState.IsButtonUp(key))
                {
                    returnButtons.Add(key);
                }
            }

            return returnButtons;
        }

        public override void Update(GameTime gameTime)
        {
            _prevState = GamePad.GetState((PlayerIndex)_gamePadIndex);
        }
    }
}
